export function jsonFormatString(data: any) {
  return JSON.stringify(data, null, 2)
}

export function jsonToCSV(jsonData: any[], head: string[], filename: string) {
  let str = `${head.join(',')}\n`

  for (let i = 0; i < jsonData.length; i++) {
    const row = []

    for (const item in jsonData[i]) {
      row.push(jsonData[i][item])
    }

    str += `${row.join(',')}\n`
  }
  // encodeURIComponent解决中文乱码
  const uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(str)

  const link = document.createElement('a')
  link.href = uri

  link.download = `${filename}.csv`
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
}
